using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Common;
using Garlic.CC.Utility;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using Garlic.DM.DataModel;

namespace Garlic.DAL
{
    /// <summary>
    /// DataAccess
    /// </summary>
    public partial class CodeListDAL
    {
        //your code should type here...
        #region Sql const
        public const string TC_BAS_CODELIST_INSERT_WITHCHECK = " INSERT INTO TC_BAS_CodeList (" +
                                            "code_cdescription," +
                                            "code_edescription," +
                                            "code_name," +
                                            "create_user," +
                                            "create_time," +
                                            "modify_user," +
                                            "modify_time" +
                                        ") VALUES (" +
                                            ":code_cdescription," +
                                            ":code_edescription," +
                                            ":code_name," +
                                            ":create_user," +
                                            ":create_time," +
                                            ":modify_user," +
                                            ":modify_time" +
                                        ")";

        public const string TC_BAS_CODELIST_Check_Have_Name = " DECLARE @Result INT;" +
                                    " IF EXISTS( " +
                                    " SELECT TOP 1 * " +
                                    " FROM TC_BAS_CodeList" +
                                    " WHERE (" +
                                    " code_name =  :code_name))" +
                                    " SET @Result = 1; " +
                                    " ELSE " +
                                    " SET @Result = 0; SELECT @Result;";

        public const string TC_BAS_CODELIST_UPDATE_WITH_NOTFULL_PARAMETERS = " UPDATE TC_BAS_CodeList SET " +
                                            " code_cdescription =  :code_cdescription, " +
                                            " code_edescription =  :code_edescription, " +
                                            " modify_user =  :modify_user, " +
                                            " modify_time =  :modify_time " +
                                        " WHERE 1=1 " +
                                            " and  code_name =  :code_name";
        #endregion

        /// <summary>
        /// Add
        /// </summary>
        /// <param name="codeListInfo"> entity</param>
        public static void AddWithCheck(CodeListInfo codeListInfo)
        {
            Database db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = db.GetSqlStringCommand(TC_BAS_CODELIST_INSERT);

            db.AddInParameter(dbCommand, "code_cdescription", DbType.AnsiString, codeListInfo.Code_cdescription);
            db.AddInParameter(dbCommand, "code_edescription", DbType.AnsiString, codeListInfo.Code_edescription);
            db.AddInParameter(dbCommand, "code_name", DbType.AnsiString, codeListInfo.Code_name);
            db.AddInParameter(dbCommand, "create_user", DbType.Int32, DBConvert.ToDBValue(codeListInfo.Create_user));
            db.AddInParameter(dbCommand, "create_time", DbType.DateTime, DBConvert.ToDBValue(codeListInfo.Create_time));
            db.AddInParameter(dbCommand, "modify_user", DbType.Int32, DBConvert.ToDBValue(codeListInfo.Modify_user));
            db.AddInParameter(dbCommand, "modify_time", DbType.DateTime, DBConvert.ToDBValue(codeListInfo.Modify_time));

            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// Check_Have_Name
        /// </summary>
        /// <param name="codeListInfo"> entity</param>
        public static int CheckHaveName(CodeListInfo codeListInfo)
        {
            Database db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = db.GetSqlStringCommand(TC_BAS_CODELIST_Check_Have_Name);

            db.AddInParameter(dbCommand, "code_name", DbType.String, codeListInfo.Code_name);

            return int.Parse("0" + db.ExecuteScalar(dbCommand));
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="codeListInfo"> utility</param>
        public static void UpdateWithNotFullParameters(CodeListInfo codeListInfo)
        {
            Database db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = db.GetSqlStringCommand(TC_BAS_CODELIST_UPDATE_WITH_NOTFULL_PARAMETERS);

            db.AddInParameter(dbCommand, "code_cdescription", DbType.AnsiString, codeListInfo.Code_cdescription);
            db.AddInParameter(dbCommand, "code_edescription", DbType.AnsiString, codeListInfo.Code_edescription);
            db.AddInParameter(dbCommand, "code_name", DbType.AnsiString, codeListInfo.Code_name);
            db.AddInParameter(dbCommand, "modify_user", DbType.Int32, DBConvert.ToDBValue(codeListInfo.Modify_user));
            db.AddInParameter(dbCommand, "modify_time", DbType.DateTime, DBConvert.ToDBValue(codeListInfo.Modify_time));

            db.ExecuteNonQuery(dbCommand);
        }
    }
}
